package com.smasher.widget.design

class PatternChain : Handler() {

    override fun doHandle() {
        next()
    }

    class Builder {

        private val list = ArrayList<Handler>()

        fun addHandler(handler: Handler): Builder {
            list.add(handler)
            return this
        }

        fun build(): Handler {
            if (list.isNotEmpty()) {
                val size = list.size
                for (index in 0 until (size - 1)) {
                    val handler = list[index]
                    val nextHandler = list[index + 1]
                    handler.addNext(nextHandler)
                }
            }

            return PatternChain().apply {
                if (list.isNotEmpty()) {
                    addNext(list[0])
                }
            }
        }
    }

}